package com.juyu.miliao.scheduler.message;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.constant.GiftWallRabbitMqInfo;
import com.juyu.miliao.scheduler.domin.GiftSpend;
import com.juyu.miliao.scheduler.service.GiftWallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author xuanqicheng
 * @create 2019-28-09:51
 */
@Component
@Slf4j
public class GiftWallMessage {

    @Autowired
    private GiftWallService giftWallService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(GiftWallRabbitMqInfo.GIFT_WALL_QUEUE),
            exchange = @Exchange(GiftWallRabbitMqInfo.GIFT_WALL_EXCHANGE),
            key =GiftWallRabbitMqInfo.GIFT_WALL_KEY))
    public void getGiftWallMessage(String message){
        if(!StringUtils.isEmpty(message)){

            try {
                GiftSpend giftSpend = JSONObject.parseObject(message, GiftSpend.class);
                if(giftSpend.getSpend()>=GiftWallRabbitMqInfo.GOLD){
                    giftWallService.setGiftWallInfo(giftSpend);
                }
            }catch (Exception ex){
                log.error("处理礼物墙原始数据错误："+ex.getMessage());
            }
        }
    }
}
